package com.ruoyi.teacher.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.teacher.domain.TTeacher;
import com.ruoyi.teacher.mapper.TNewspaperMapper;
import com.ruoyi.teacher.mapper.TTeacherMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teacher.mapper.TExceptionMapper;
import com.ruoyi.teacher.domain.TException;
import com.ruoyi.teacher.service.ITExceptionService;

/**
 * 老师日报Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-24
 */
@Slf4j
@Service
public class TExceptionServiceImpl implements ITExceptionService 
{
    @Autowired
    private TExceptionMapper tExceptionMapper;
    @Autowired
    private TTeacherMapper tTeacherMapper;
    @Autowired
    private TNewspaperMapper tNewspaperMapper;

    /**
     * 查询老师日报
     * 
     * @param id 老师日报主键
     * @return 老师日报
     */
    @Override
    public TException selectTExceptionById(Long id)
    {
        return tExceptionMapper.selectTExceptionById(id);
    }

    /**
     * 查询老师日报列表
     * 
     * @param tException 老师日报
     * @return 老师日报
     */
    @Override
    public List<TException> selectTExceptionList(TException tException)
    {
        return tExceptionMapper.selectTExceptionList(tException);
    }

    /**
     * 新增老师日报
     * 
     * @param tException 老师日报
     * @return 结果
     */
    @Override
    public int insertTException(TException tException)
    {
        tException.setCreateTime(DateUtils.getNowDate());
        return tExceptionMapper.insertTException(tException);
    }

    /**
     * 修改老师日报
     * 
     * @param tException 老师日报
     * @return 结果
     */
    @Override
    public int updateTException(TException tException)
    {
        return tExceptionMapper.updateTException(tException);
    }

    /**
     * 批量删除老师日报
     * 
     * @param ids 需要删除的老师日报主键
     * @return 结果
     */
    @Override
    public int deleteTExceptionByIds(Long[] ids)
    {
        return tExceptionMapper.deleteTExceptionByIds(ids);
    }

    /**
     * 删除老师日报信息
     * 
     * @param id 老师日报主键
     * @return 结果
     */
    @Override
    public int deleteTExceptionById(Long id)
    {
        return tExceptionMapper.deleteTExceptionById(id);
    }

    @Override
    public void startExceptionTasks() {
        List<Long> teacherIds = tTeacherMapper.selectTeacherId();
        // 查询昨日填写日报的老师id
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date time = cal.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String yesterday = simpleDateFormat.format(time);
        List<Long> PaperTeacherIds = tNewspaperMapper.selectTeacherId(yesterday);
        teacherIds.removeAll(PaperTeacherIds);
        // 查找出没打卡的老师信息
        List<TTeacher> teachers = tTeacherMapper.selectTeacherByIds(teacherIds);
        Date date = new Date();
        batchAddException(time, teachers, yesterday);
        log.info(simpleDateFormat.format(date) + " 日志异常已执行！");
    }


    /**
     * 批量添加异常日志
     * @param time
     * @param teachers
     */
    private void batchAddException(Date time, List<TTeacher> teachers, String yesterday) {
        TException tException;
        for (TTeacher teacher : teachers) {
            tException = new TException();
            tException.setPaperTime(time);
            String name = teacher.getName();
            tException.setTeacherName(teacher.getName());
            tException.setIntro("【" + name + "】老师【"+ yesterday + "】未填写日志！");
            tException.setCreateTime(new Date());
            tExceptionMapper.insertTException(tException);
        }
    }
}
